package cz.semjob.learning.classifier.benchmark;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import weka.classifiers.Classifier;
import cz.semjob.document.annotation.LabeledRelationWordsPair;
import cz.semjob.document.annotation.RelationWordsPair;
import cz.semjob.document.annotation.metadata.Relation;
import cz.semjob.document.util.DocumentUtil;
import cz.semjob.learning.relation.IRelationInstanceCreator;

public class ClassifierRelationBenchmarkResults extends
		ClassifierBenchmarkResults {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8082883762794706604L;

	private List<Relation> relations;

	private IRelationInstanceCreator relationInstanceCreator;

	private List<ClassifierRelationBenchmarkResult> documentResults;

	private String relationType;

	public ClassifierRelationBenchmarkResults() {
		super();
	}

	public ClassifierRelationBenchmarkResults(List<Relation> relations,
			Classifier classifier,
			IRelationInstanceCreator relationInstanceCreator,
			String classifierInfo) {
		super();
		this.relations = relations;
		this.documentResults = new ArrayList<ClassifierRelationBenchmarkResult>();
		this.classifier = classifier;
		this.relationInstanceCreator = relationInstanceCreator;
		this.classifierInfo = classifierInfo;
		this.relationType = relations.get(0).getType();
	}

	public List<Relation> getRelations() {
		return relations;
	}

	public void addResult(String documentName, double precision, double recall,
			double trueNegativeRate, double accuracy,
			Map<Relation, List<LabeledRelationWordsPair>> foundRelations) {
		this.documentResults.add(new ClassifierRelationBenchmarkResult(
				documentName, precision, recall, trueNegativeRate, accuracy,
				foundRelations));
	}

	public IRelationInstanceCreator getInstanceCreator() {
		return relationInstanceCreator;
	}

	public String toPrettyString() {
		StringBuilder sb = new StringBuilder();

		sb.append("ClassifierBenchmarkResults for relations: "
				+ relations.toString() + "\n");

		sb.append(this.overallResult.toString() + "\n");
		sb.append(dataSetStatistics.toString());
		sb.append("\n");
		for (ClassifierRelationBenchmarkResult result : this.documentResults) {
			sb.append(result.toString());
			for (Relation relation : result.getRelations().keySet())
				for (RelationWordsPair resultWords : result.getRelations().get(
						relation)) {
					sb.append(relation.getName()
							+ ":"
							+ DocumentUtil.printWords(resultWords
									.getRelationWord1())
							+ ", "
							+ DocumentUtil.printWords(resultWords
									.getRelationWord2()) + "\n");
				}
			sb.append("\n");
		}
		sb.append("\n");
		return sb.toString();
	}

	@Override
	public String toInfoString() {
		StringBuilder info = new StringBuilder();
		info.append("relationType=" + this.relationType + "\n");
		info.append("classifier=" + this.classifierInfo + "\n");
		info.append("accuracy=" + this.overallResult.getAccuracy() + "\n");
		info.append("precision=" + this.overallResult.getPrecision() + "\n");
		info.append("recall=" + this.overallResult.getRecall() + "\n");
		info.append("fmeasure=" + this.overallResult.getFMeasure() + "\n");
		return info.toString();
	}

	public String getRelationType() {
		return relationType;
	}
}
